package com.tjetc.service.impl;

import com.tjetc.domain.Admin;
import com.tjetc.util.InitUsername;
import com.tjetc.util.JwtUtils;
import com.tjetc.vo.AdminLoginVo;
import com.tjetc.vo.DoctorLoginVo;
import com.tjetc.vo.Result;
import com.aliyuncs.exceptions.ClientException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tjetc.util.AliYunOss;
import com.tjetc.vo.Result;
import com.tjetc.domain.Admin;
import com.tjetc.util.JwtUtils;
import com.tjetc.vo.AdminLoginVo;
import com.tjetc.vo.DoctorLoginVo;
import com.tjetc.vo.Result;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.print.Doc;

import com.tjetc.mapper.DoctorMapper;
import com.tjetc.domain.Doctor;
import com.tjetc.service.DoctorService;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;

@Service
public class DoctorServiceImpl implements DoctorService{

    @Resource
    private DoctorMapper doctorMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return doctorMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Doctor record) {
        return doctorMapper.insert(record);
    }

    @Override
    public int insertSelective(Doctor record) {
        return doctorMapper.insertSelective(record);
    }

    @Override
    public Doctor selectByPrimaryKey(Integer id) {
        return doctorMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Doctor record) {
        return doctorMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Doctor record) {
        return doctorMapper.updateByPrimaryKey(record);
    }

    @Override
    public Result selByClinicId(Integer cid) {
        List<Doctor> doctors =  doctorMapper.selByClinicId(cid);
        return Result.ok(doctors);
    }

    @Override
    public Result login(Doctor doctor) {
        String username = doctor.getUsername();
        String password = doctor.getPassword();
        Doctor oldDoctor = doctorMapper.selectByUsername(username);
        if (oldDoctor == null){
            return Result.fail("该用户不存在");
        }
        if (!oldDoctor.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))){
            return Result.fail("账号和密码不匹配");
        }
        oldDoctor.setPassword("******");
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",oldDoctor.getId());
        String token = JwtUtils.createJwtToken(map);
        DoctorLoginVo doctorLoginVo = new DoctorLoginVo();
        doctorLoginVo.setDoctor(oldDoctor);
        doctorLoginVo.setToken(token);
        return Result.ok(doctorLoginVo);
    }

    @Override
    public Result getAll(Integer hid, String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Doctor> list = doctorMapper.getAll(name, hid);
        return list==null?Result.fail():Result.ok(new PageInfo<>(list));
    }

    @Override
    @Transactional
    public Result save(Doctor doctor, MultipartFile file) throws IOException, ClientException {
        String initPassword="123456";
        if (file!=null ){
            try {
                String upload = AliYunOss.upload(file.getBytes(), file.getOriginalFilename());
                doctor.setIcon(upload);
            } catch (Exception e) {
                return Result.fail();
            }
        }

        if (doctor.getId()==null){
            doctor.setUsername(InitUsername.generateRandomAlphanumeric(7));
            doctor.setPassword(DigestUtils.md5DigestAsHex(initPassword.getBytes()));
            return doctorMapper.insert(doctor)==1?Result.ok():Result.fail();
        }else {
            return doctorMapper.updateByPrimaryKeySelective(doctor)==1?Result.ok():Result.fail();
        }
    }

    @Override
    public Result deleteById(Integer id) {
        return doctorMapper.deleteByPrimaryKey(id)==1?Result.ok():Result.fail();
    }

    @Override
    public Doctor getDoctorAndClinic(Integer id) {
        return doctorMapper.getDoctorAndClinic(id);
    }

    @Override
    @Transactional
    public Result updatePassword(Integer id, String oldPassword, String newPassword) {

        Doctor doctor = doctorMapper.selectByPrimaryKey(id);
        if (doctor!= null){
            if (doctor.getPassword().equals(DigestUtils.md5DigestAsHex(oldPassword.getBytes()))){
                doctor.setPassword(DigestUtils.md5DigestAsHex(newPassword.getBytes()));
                return doctorMapper.updateByPrimaryKeySelective(doctor)==1?Result.ok("修改成功"):Result.fail("修改失败");
            }else {
                return Result.fail("原密码错误");
            }
        }else {
            return Result.fail("数据异常,请重新登录");
        }
    }


}
